package selenium;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class tengxun5 {
	// 切割腾讯视频作者为每个txt里面只要2000个链接
	public static void main(String[] args) {
		
		String filepath = "D:\\腾讯视频按作者采集/";// 需要处理的文件夹目录
		String fileName = "2017年12月16日11时24分37秒.txt";// 需要处理的文件夹目录
		Date now = new Date();// 获取系统当前时间
		SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");// 到日期为止
		SimpleDateFormat ss = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
		String strDate = s.format(now);// 格式化日期转换为字符串
		String strDate1 = ss.format(now);// 格式化日期转换为字符串
		String fileExist1 = filepath +strDate;
		File fileExist2 = new File(fileExist1);
		if (!fileExist2.exists()) {// 判断目录是否存在
			fileExist2.mkdirs();
		}
		try {
			List<String> list = readFileAll("D:\\腾讯视频按作者采集\\" + strDate + ".txt");
			ArrayList<String> ts = new ArrayList<String>();
			for (String string : list) {
				if (!ts.contains(string)) {
					ts.add(string);
				}
			}
			for (String aaline : ts) {
				method22("D:\\腾讯视频按作者采集\\" + strDate1 + ".txt", aaline);
			}
		} catch (IOException e) {
			System.out.println("排序错误||");
			e.printStackTrace();
		}
		
		try {
			List<String> list = readFileAll(filepath + strDate1 + ".txt");
			List <String > lst = new ArrayList<String>();
			for (int j = 0; j < list.size(); j++) {
				lst.add(list.get(j));
				if (lst.size()==10|| j == list.size()-1) {
					writeFile(filepath +strDate+"/"+ strDate1 + j + ".txt", lst);
					lst.clear();
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

	// 方法1：

	public void method1() {
		FileWriter fw = null;
		try {
			// 如果文件存在，则追加内容；如果文件不存在，则创建文件
			File f = new File("E:\\dd.txt");
			fw = new FileWriter(f, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		PrintWriter pw = new PrintWriter(fw);
		pw.println("追加内容");
		pw.flush();
		try {
			fw.flush();
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	// 方法2：

	public static void method2(String file, String conent) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
			out.write(conent + "\r\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public static void method22(String file, String conent) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
			out.write(conent);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	// 方法3：

	public static void method3(String fileName, String content) {
		try {
			// 打开一个随机访问文件流，按读写方式
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
			// 文件长度，字节数
			long fileLength = randomFile.length();
			// 将写文件指针移到文件尾。
			randomFile.seek(fileLength);
			randomFile.writeBytes(content + "\r\n");
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static List<String> readFile(String filePath, String str) throws IOException {
		File file = new File(filePath);
		BufferedReader reader = null;
		List<String> list = new ArrayList<String>();
		reader = new BufferedReader(new FileReader(file));
		String text = reader.readLine();
		while (text != null) {
			if (!text.trim().equals(str)) {
				list.add(text + "\r\n");
			}
			text = reader.readLine();
		}
		reader.close();
		return list;
	}
	
	public static List<String> readFileAll(String filePath) throws IOException {
		File file = new File(filePath);
		BufferedReader reader = null;
		List<String> list = new ArrayList<String>();
		reader = new BufferedReader(new FileReader(file));
		String text = reader.readLine();
		while (text != null) {
			list.add(text + "\r\n");
			text = reader.readLine();
		}
		reader.close();
		return list;
	}

	public static void writeFile(String filePath, List<String> list) throws IOException {
		File file = new File(filePath);
		if (!file.exists()) {
			file.createNewFile();
		}
		FileOutputStream outputStream = new FileOutputStream(file);
		for (String s : list) {
			outputStream.write(s.getBytes());
		}
		outputStream.close();
	}
	
	public static void writeFileSet(String filePath, Set<String> list) throws IOException {
		File file = new File(filePath);
		if (!file.exists()) {
			file.createNewFile();
		}
		FileOutputStream outputStream = new FileOutputStream(file);
		for (String s : list) {
			outputStream.write(s.getBytes());
		}
		outputStream.close();
	}
}
